package com.application.Activity;

import java.util.ArrayList;

import org.w3c.dom.Document;

import it.unipr.ce.dsg.s2p.sip.Address;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;

import ReadWriteXML.CMember;
import ReadWriteXML.CNeighbor;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FrmInteraction extends Activity {

	private Button _btnSendMessage;
	private EditText _edtContentMessage;
	private Button _btnRequestTest;
	private Button _btnBack;
	private ArrayList<CNeighbor> _listNeighbor;
	Document _doc = null;
	static String _pathNeighList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_interaction);

		// -----------khoi tao cac control-------------
		_btnSendMessage = (Button) findViewById(R.id.interaction_btnsendmessage);
		_edtContentMessage = (EditText) findViewById(R.id.interaction_edtmessage);
		_btnRequestTest = (Button) findViewById(R.id.interaction_btnrequesttest);
		_btnBack = (Button) findViewById(R.id.interaction_btnback);

		_pathNeighList = ReadWriteFileController.DIRECTORY + "NeighborsList.xml";
		_listNeighbor=readNeighbor();
		// ------------bat su kien khi click nut button--------------
		_btnSendMessage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// gửi thông điệp yêu cầu
				
			}
		});

		// -----------bat su kien khi click vao nut tin kiem bai tap------------
		_btnRequestTest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// cac thao tac de tim kiem bai tap
				Toast toast = Toast.makeText(getBaseContext(),
						" vao ham gui thong diep tim bai tap", Toast.LENGTH_SHORT);
				toast.show();
				for (int i = 0; i < _listNeighbor.size(); i++) {
					//------gui thong diep xin bai tap--------
					String s = _listNeighbor.get(i).get_member().get_ip().toString();
					NetworkController.SendMessageRequestExercises(new Address(s));
					Log.d("PEERDROID", "IP dc moi la: " + s);
				}

			}
		});

	}
	private ArrayList<CNeighbor> readNeighbor() {
		ArrayList<CNeighbor> listNeighbor = new ArrayList<CNeighbor>();
		ArrayList<CMember> listMember = new ArrayList<CMember>();
		_doc = ReadXML.BuildXML(_pathNeighList);
		listMember = ReadXML.ReadMember(_doc);
		boolean temp=false;
		
		for(int i=0;i<listMember.size();i++){
			CMember mem = new CMember();
			mem=listMember.get(i);
			CNeighbor neighbor=new CNeighbor(mem, temp);
			listNeighbor.add(neighbor);
		}
		return listNeighbor;

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
		ActivityController.setPeerActivity(this);
	}
}
